<template>
  <div class="head-wrap">
    <div class="nav-topbar">
      <div class="container">
        <div class="menu-left">
          <a href="javascript:;">小米商城</a>
          <a href="javascript:;">MIUI</a>
          <a href="javascript:;">云服务</a>
          <a href="javascript:;">协议规则</a>
        </div>
        <div class="menu-right">
          <a href="javascript:;" v-if="username">{{username}}</a>
          <router-link to="/login" tag="a" href="javascript:;" v-if="!username" >登录</router-link>
          <a href="javascript:;" v-if="username" @click="logout">退出</a>
          <a href="/#/order/list" v-if="username">我的订单</a>
          <router-link to="/cart" tag="a" href="javascript:;" class="my-car"><span class="icon-car"></span>购物车</router-link>
        </div>
      </div>
    </div>

    <div class="nav-header">
      <div class="container">
        <div class="head-logo">
          <a href="/#/index"></a>
        </div>
        <div class="head-menu">
          <div class="menu-item">
            <span>小米手机</span>
            <div class="children">
              <ul>
                <li class="product" v-for="(item,index) in productList" :key="index">
                  <a href="" target="_blank">
                    <div class="product-img">
                      <img :src="item.mainImage" alt="">
                    </div>
                    <div class="product-name">{{item.name}}</div>
                    <div class="product-price">{{item.price | currency}}</div>
                  </a>
                </li>
              </ul>
            </div>
          </div>
          <div class="menu-item">
            <span>Redmi红米</span>
            <div class="children">
              <ul>
                <li class="product">
                  <a href="" target="_blank">
                    <div class="product-img">
                      <img src="../../../public/imgs/item-box-1.png" alt="">
                    </div>
                    <div class="product-name">小米CC9</div>
                    <div class="product-price">1799元</div>
                  </a>
                </li>
                <li class="product">
                  <a href="" target="_blank">
                    <div class="product-img">
                      <img src="../../../public/imgs/item-box-1.png" alt="">
                    </div>
                    <div class="product-name">小米CC9</div>
                    <div class="product-price">1799元</div>
                  </a>
                </li>
                <li class="product">
                  <a href="" target="_blank">
                    <div class="product-img">
                      <img src="../../../public/imgs/item-box-1.png" alt="">
                    </div>
                    <div class="product-name">小米CC9</div>
                    <div class="product-price">1799元</div>
                  </a>
                </li>
                <li class="product">
                  <a href="" target="_blank">
                    <div class="product-img">
                      <img src="../../../public/imgs/item-box-1.png" alt="">
                    </div>
                    <div class="product-name">小米CC9</div>
                    <div class="product-price">1799元</div>
                  </a>
                </li>
                <li class="product">
                  <a href="" target="_blank">
                    <div class="product-img">
                      <img src="../../../public/imgs/item-box-1.png" alt="">
                    </div>
                    <div class="product-name">小米CC9</div>
                    <div class="product-price">1799元</div>
                  </a>
                </li>
                <li class="product">
                  <a href="" target="_blank">
                    <div class="product-img">
                      <img src="../../../public/imgs/item-box-1.png" alt="">
                    </div>
                    <div class="product-name">小米CC9</div>
                    <div class="product-price">1799元</div>
                  </a>
                </li>
              </ul>
            </div>
          </div>
          <div class="menu-item">
            <span>电视</span>
            <div class="children">
              <ul>
                <li class="product">
                  <a href="" target="_blank">
                    <div class="product-img">
                      <img src="../../../public/imgs/item-box-1.png" alt="">
                    </div>
                    <div class="product-name">小米CC9</div>
                    <div class="product-price">1799元</div>
                  </a>
                </li>
                <li class="product">
                  <a href="" target="_blank">
                    <div class="product-img">
                      <img src="../../../public/imgs/item-box-1.png" alt="">
                    </div>
                    <div class="product-name">小米CC9</div>
                    <div class="product-price">1799元</div>
                  </a>
                </li>
                <li class="product">
                  <a href="" target="_blank">
                    <div class="product-img">
                      <img src="../../../public/imgs/item-box-1.png" alt="">
                    </div>
                    <div class="product-name">小米CC9</div>
                    <div class="product-price">1799元</div>
                  </a>
                </li>
                <li class="product">
                  <a href="" target="_blank">
                    <div class="product-img">
                      <img src="../../../public/imgs/item-box-1.png" alt="">
                    </div>
                    <div class="product-name">小米CC9</div>
                    <div class="product-price">1799元</div>
                  </a>
                </li>
                <li class="product">
                  <a href="" target="_blank">
                    <div class="product-img">
                      <img src="../../../public/imgs/item-box-1.png" alt="">
                    </div>
                    <div class="product-name">小米CC9</div>
                    <div class="product-price">1799元</div>
                  </a>
                </li>
                <li class="product">
                  <a href="" target="_blank">
                    <div class="product-img">
                      <img src="../../../public/imgs/item-box-1.png" alt="">
                    </div>
                    <div class="product-name">小米CC9</div>
                    <div class="product-price">1799元</div>
                  </a>
                </li>
              </ul>
            </div>
          </div>
        </div>
        <div class="head-search">
          <div class="wrapper">
            <input type="text" name="keyword">
            <a href="javascript:;"></a>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {mapState} from 'vuex'
export default {
name: "NavHeader",
  data(){
  return{
    productList:[]
  }
  },
  computed:{
    ...mapState(['username'])
  },
  methods:{
    getProductList(){
      this.axios.get('/products',{
        params:{
          categoryId:'100012',
          pageSize:6
        }
      }).then(res=>{
        // console.log(res.list)
        this.productList = res.list;
      })
    },
    logout(){
      this.axios.post('/user/logout').then(()=>{
        console.log('退出成功');
        this.$cookie.set('userId','',{expires:'-1'});
        this.$store.dispatch('saveUserName','');
      })
    }
  },
  created() {
  this.getProductList()
  },
  filters:{
    currency(val){
      return `￥${val}元`
    }
  }
}
</script>

<style lang="scss" scoped>
@import "src/assets/scss/reset.scss";
@import "src/assets/scss/base.scss";
@import "src/assets/scss/mixin.scss";
@import "src/assets/scss/config.scss";
.head-wrap{
  .nav-topbar{
    height: 39px;
    line-height: 39px;
    background-color: #333333;
    color: #B0B0B0;
    .container{
      @include flex();
      a{
        display: inline-block;
        color: #B0B0B0;
        margin-right: 17px;
      }
      .my-car{
        width: 110px;
        background-color: #FF6600;
        text-align: center;
        color: #ffffff;
        margin-right: 0;
        .icon-car{
          margin-right: 4px;
          @include bgImg(16px,12px,'../../../public/imgs/icon-cart-checked.png');
        }
      }
    }
  }
  .nav-header{
    .container{
      position: relative;
      @include flex();
      .head-logo{
        width: 55px;
        height: 55px;
        background-color: #FF6600;
        //border: 1px solid #FF6600;
        a{
          display: inline-block;
          width: 110px;
          height: 100%;
          &:before{
            content: '';
            @include bgImg(55px,55px,'../../../public/imgs/mi-logo.png',55px);
            transition: margin .2s;
          }
          &:after{
            content: '';
            @include bgImg(55px,55px,'../../../public/imgs/mi-home.png',55px);
          }
          &:hover:before{
            margin-left: -55px;
            transition: margin .2s;
          }
        }
      }
      .head-menu{
        display: inline-block;
        width: 643px;
        padding-left: 209px;
        .menu-item{
          display: inline-block;
          color: #333333;
          font-weight: bold;
          font-size: 16px;
          line-height: 112px;
          margin-right: 20px;
          span{
            cursor: pointer;
          }
          &:hover{
            color: #FF6600;
            .children{
              height: 220px;
              opacity: 1;
            }
          }
          .children{
            position: absolute;
            top:112px;
            left: 0;
            width: 1126px;
            height: 0;
            opacity: 0;
            overflow: hidden;
            border-top:1px solid #E5E5E5;
            box-shadow:0px 7px 6px 0px rgba(0, 0, 0, 0.11);
            z-index: 100;
            background-color: #ffffff;
            transition: all .5s;
            .product{
              position: relative;
              width: 16.6%;
              height: 220px;
              display: inline-block;
              font-size: 12px;
              line-height: 12px;
              text-align: center;
              a{
                display: inline-block;
              }
              img{
                height: 111px;
                width: auto;
                margin-top: 26px;
              }
              .product-img{height: 137px}
              .product-name{
                font-weight: bold;
                margin-top: 16px;
                margin-bottom: 8px;
                color: $colorB;
              }
              .product-price{
                color:$colorA;
              }
              &:after{
                position: absolute;
                content: '';
                top:28px;
                right:0;
                border-left:1px solid $colorF;
                height:100px;
                width:1px;
              }
            }
          }
        }
      }
      .head-search{
        width: 319px;
        .wrapper{
          height: 50px;
          display: flex;
          align-items: center;
          border: 1px solid #E0E0E0;
          input{
            width: 264px;
            height: 50px;
            border: none;
            padding-left: 17px;
            box-sizing: border-box;
            border-right:1px solid #E0E0E0;
          }
          a{
            @include bgImg(18px,18px,'../../../public/imgs/icon-search.png');
            margin-left: 17px;
            margin-right: 17px;
          }
        }
      }
    }
  }
}
</style>
